package com.xsmart.oa.services;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.common.entity.user.AdminUser;
import com.xsmart.common.services.LoginTokenService;
import com.xsmart.oa.dao.AdminLoginDAO;
import com.xsmart.oa.dao.AdminPowerSettingDAO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminLoginService {
    @Autowired
    AdminLoginDAO adminLoginDAO;
    @Autowired
    LoginTokenService loginTokenService;
    @Autowired
    AdminPowerSettingDAO adminPowerSettingDAO;
    @Autowired
    AdminMenuService adminMenuService;
    /**
     * 登录验证
     * @param requestBean
     * @return
     */
    public ResponseBean login(RequestBean requestBean) {
        String userName = requestBean.getRequestParamString("userName");
        String password = requestBean.getRequestParamString("password");
        String remember = requestBean.getRequestParamString("remember");
        boolean isRemember = "true".equals(remember);
        Map<String, Object> adminUser = adminLoginDAO.login(userName, password);
        ResponseBean responseBean = requestBean.getResponseBean();
        if (adminUser != null) {
            adminUser.remove("password");
            adminUser.put("loginTime",System.currentTimeMillis());
            String token = loginTokenService.generalTokenStr();
            loginTokenService.saveLoginToken(token, adminUser, isRemember);
            List<String> actionCodeList = adminPowerSettingDAO.queryActionCode(MapUtils.getInteger(adminUser, "user_id"));
            //获取用户菜单
            List<Map<String, Object>> menuList = adminMenuService.queryAdminMenu(MapUtils.getString(adminUser, "user_id"));
            responseBean.setResult(ResultConstant.SUCCESS);
            responseBean.addResultItem("adminMenu", menuList);
            responseBean.addResultItem("actionCodeList",actionCodeList);
            responseBean.addResultItem("userInfo", adminUser);
            responseBean.setToken(token);
        } else {
            responseBean.setResult(ResultConstant.USER_PASSWORD_INVALID);
        }
        return responseBean;
    }

    /**
     * 退出登录
     * @param requestBean
     * @return
     */
    public ResponseBean logout(RequestBean requestBean) {
        String token = requestBean.getToken();
        loginTokenService.clearLoginToken(token);
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     *验证登录状态
     * @param requestBean
     * @return
     */
    public ResponseBean checkLogin(RequestBean requestBean) {
        String token = requestBean.getToken();
        ResponseBean responseBean = requestBean.getResponseBean();
        if (loginTokenService.checkUserLoginByToken(token)) {
            Map<String, String> userInfo = loginTokenService.getLoginUserInfoByToken(token);
            int userId=requestBean.getRequestParamInteger("userId",-1);
            if(userId==-1){
                responseBean.setResult(ResultConstant.ERROR);
                responseBean.setMessage("错误的用户信息");
            }else if(userId!= MapUtils.getIntValue(userInfo,"user_id",-1)){
                responseBean.setResult(ResultConstant.ERROR);
                responseBean.setMessage("错误的用户信息");
            }else {
                responseBean.setResult(ResultConstant.SUCCESS);
                responseBean.addResultItem("userInfo", userInfo);
                responseBean.setToken(token);
            }
        } else {
            responseBean.setResult(ResultConstant.LOGIN_TIME_OUT);
        }
        return responseBean;
    }
}
